<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="mini" label-width="100px">
          <el-form-item label="车辆海关编号" prop="veCustomsNo">
            <el-input v-model.trim="queryParams.veCustomsNo" placeholder="请输入车辆海关编号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="境内车牌号" prop="domesticVehicleNo">
            <el-input v-model.trim="queryParams.domesticVehicleNo" placeholder="请输入境内车牌号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="境外车牌号" prop="abroadVehicleNo">
            <el-input v-model.trim="queryParams.abroadVehicleNo" placeholder="请输入境外车牌号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="车辆类型" prop="vehicleType">
            <el-select v-model.trim="queryParams.vehicleType" placeholder="请选择车辆类型" clearable>
              <el-option v-for="dict in dict.type.sys_vehicle_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="IC卡号" prop="cardNo">
            <el-input v-model.trim="queryParams.cardNo" placeholder="请输入IC卡号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="电子车牌" prop="rfid">
            <el-input v-model.trim="queryParams.rfid" placeholder="请输入电子车牌" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="申报状态" prop="declStatus">
            <el-select v-model.trim="queryParams.declStatus" placeholder="请选择申报状态" clearable>
              <el-option v-for="dict in dict.type.vehicle_decl_status" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="使用状态" prop="useStatus">
            <el-select v-model.trim="queryParams.useStatus" placeholder="请选择使用状态" clearable>
              <el-option v-for="dict in dict.type.use_status" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="申报日期">
            <el-date-picker v-model="daterangeDeclTime" class="reset-clear" type="datetimerange" :clearable="false"
              unlink-panels start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']" />
          </el-form-item>

        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd('add')"
            v-hasPermi="['billsdecl:preVehicleRecord:add']">新增</el-button>
        </el-col>
        <el-button type="primary"  plain size="mini" icon="el-icon-edit" :disabled="multiple" @click="handleEdit('edit')"
          v-hasPermi="['billsdecl:preVehicleRecord:edit']">
          编辑
        </el-button>
        <el-button type="primary"  plain size="mini" icon="el-icon-upload2" :disabled="multiple" @click="handleDecl"
          v-hasPermi="['billsdecl:preVehicleRecord:decl']">
          申报
        </el-button>
        <el-button type="primary"  plain size="mini" icon="el-icon-refresh-right" :disabled="multiple" @click="handleDeclRepeat"
          v-hasPermi="['billsdecl:preVehicleRecord:decl']">
          重发(申报)
        </el-button>
        <el-button type="primary"  plain size="mini" icon="el-icon-download" :disabled="multiple" @click="handleChargeback"
          v-hasPermi="['billsdecl:preVehicleRecord:chargeback']">
          退单
        </el-button>
        <el-button type="primary"  plain size="mini" icon="el-icon-download" :disabled="single" @click="handleBindCard"
          v-hasPermi="['billsdecl:preVehicleRecord:edit']">
          绑卡
        </el-button>


        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['billsdecl:preVehicleRecord:remove']">删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['billsdecl:preVehicleRecord:export']"
          >导出</el-button>
        </el-col>
        -->

        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table ref="tableList" :columns="columns" :tableData="tableData" :total="pageParams.total"
        @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :loading="loading"
        :needCheckbox="true" @selectionChange="handleSelectionChange">


        <template #vehicleType="scope">
          <dict-value :list="dict.type.sys_vehicle_type" :dictCode="scope.row.vehicleType"></dict-value>
        </template>


        <template #declStatus="scope">
          <dict-value :list="dict.type.vehicle_decl_status" :dictCode="scope.row.declStatus"></dict-value>
        </template>


        <template #useStatus="scope">
          <dict-value :list="dict.type.use_status" :dictCode="scope.row.useStatus"></dict-value>
        </template>
        <template #sources="scope">
          <dict-value :list="dict.type.sys_vehicle_record_sources" :dictCode="scope.row.sources"></dict-value>
        </template>
        <template #regType="scope">
          <dict-value :list="dict.type.fee_config_vehicle_type" :dictCode="scope.row.regType"></dict-value>
        </template>
        <template #otherProvincesReg="scope">
          <dict-value :list="dict.type.yes_or_no" :dictCode="scope.row.otherProvincesReg"></dict-value>
        </template>


        <template #operate="scope">
          <div class="option-menu">
            <el-button type="text" size="mini" class="table-edit" @click="handleDetail(scope.row)">
              详情
            </el-button>
            <el-button type="text" size="mini" class="table-edit" @click="handleShowHis(scope.row)">
              变更记录
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit ref="addEdit" :dialogType="dialogType" :rowData="rowData" :dict="dictProp" @handleQuery="handleQuery" />
    <!-- 绑定IC卡 -->
    <bind-card ref="bindCard"  :rowData="rowData" :dict="dictProp" @handleQuery="handleQuery" />

    <!-- 详情 -->
    <detail-dialog ref="detail" :detailId="detailId" :dict="dictProp"></detail-dialog>

  </div>
</template>

<script>
  import {
    listPreVehicleRecord,
    delPreVehicleRecord,
    exportPreVehicleRecord,
    declPreVehicleRecord,
    declRepeatPreVehicleRecord,
    chargebackPreVehicleRecord
  } from "@/api/billsdecl/preVehicleRecord";
  import addEdit from "./components/addEdit";
  import bindCard from "./components/bindCard";
  import detailDialog from "./components/detailDialog";
  export default {
    name: "PreVehicleRecord",
    components: {
      addEdit,
      bindCard,
      detailDialog
    },
    dicts: ['sys_vehicle_type', 'vehicle_decl_status', 'use_status','sys_vehicle_record_sources','fee_config_vehicle_type','yes_or_no'],
    data() {
      return {
        // 显示搜索条件
        showSearch: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          veCustomsNo: null,
          domesticVehicleNo: null,
          abroadVehicleNo: null,
          vehicleType: null,
          cardNo: null,
          rfid: null,
          declStatus: null,
          useStatus: null,
          declTime: null,
        },
        // 遮罩层
        loading: true,
        // 列表数据
        tableData: [],
        columns: [{
            prop: "domesticVehicleNo",
            label: "境内车牌",
            align: "center"
          },

          {
            prop: "abroadVehicleNo",
            label: "境外车牌",
            align: "center"
          },
          {
            prop: "veCustomsNo",
            label: "车辆编号",
            align: "center"
          },
          {
            prop: "selfWt",
            label: "空车重量",
            align: "center"
          },
          {
            prop: "customsCode",
            label: "主管海关",
            align: "center"
          },
          {
            prop: "vehicleType",
            label: "车辆类型",
            align: "center",
            slot: true
          },
          {
            prop: "regType",
            label: "车辆结构",
            align: "center",
            slot: true
          },
          {
            prop: "cardNo",
            label: "IC卡号",
            align: "center"
          },
          {
            prop: "rfid",
            label: "电子车牌",
            align: "center"
          },
          {
            prop: "expirationDate",
            label: "有效期",
            align: "center"
          },
          {
            prop: "declStatus",
            label: "申报状态",
            align: "center",
            slot: true
          },
          {
            prop: "useStatus",
            label: "使用状态",
            align: "center",
            slot: true
          },
          {
            prop: "sources",
            label: "来源",
            align: "center",
            slot: true
          },
          {
            prop: "veType",
            label: "运管车型",
            align: "center",
          },
          {
            prop: "otherProvincesReg",
            label: "外省备案",
            align: "center",
            slot:true
          },

          {
            prop: "operate",
            label: "操作",
            slot: true,
            width: 140,
            fixed: "right",
          },
        ],
        pageParams: {
          total: 0,
        },
        rowData: {},
        detailId: "",
        dialogType: "",
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        //详情显示的时候，表单编辑禁用
        isViewDisabled: false,

        // 车辆备案表格数据
        preVehicleRecordList: [],
        // 审核意见时间范围
        daterangeDeclTime: [],
      };
    },
    computed: {
      dictProp() { //字典list，为避免子组件重复调接口获取，用computed计算传值
        return {
          type: this.dict.type
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询车辆备案列表 */
      getList() {
        this.loading = true;
        this.addDateRange(this.queryParams, this.daterangeDeclTime,"DeclTime")
        listPreVehicleRecord(this.queryParams).then(response => {
          this.tableData = response.rows;
          this.pageParams.total = response.total;
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.$refs.tableList.pageParams.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeDeclTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 分页
      handleSizeChange(val) {
        this.queryParams.pageSize = val.pageSize;
        this.getList();
      },
      handleCurrentChange(val) {
        this.queryParams.pageNum = val.currentPage;
        this.getList();
      },
      // 新增
      handleAdd(type) {
        this.dialogType = type;
        this.$refs.addEdit.open();
      },
      //编辑
      handleEdit(type) {
        if( this.ids.length==0){
           this.$modal.msgSuccess("请选择记录");
          return;
        }
        this.dialogType = type;
         this.rowData.id =  this.ids[this.ids.length-1];
        this.$refs.addEdit.open();
      },
      // 详情
      handleDetail(row) {
        this.detailId = row.id;
        this.$nextTick(() => {
          this.$refs.detail.open(row);
        });
      },
      //展示变更历史
      handleShowHis(row){
        this.$modal.msgSuccess("暂未实现");
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除车辆备案编号为"' + ids + '"的数据项？').then(function() {
          return delPreVehicleRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleDecl(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认申报车辆备案编号为"' + ids + '"的数据项？').then(function() {
          return declPreVehicleRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      },
      handleDeclRepeat(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认重试申报车辆备案编号为"' + ids + '"的数据项？').then(function() {
          return declRepeatPreVehicleRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      },
      handleChargeback(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认退单车辆备案编号为"' + ids + '"的数据项？').then(function() {
          return chargebackPreVehicleRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {});
      },
      handleBindCard(){
        if( this.ids.length==0){
          this.$modal.msgSuccess("请选择记录");
          return;
        }
        this.rowData.id =  this.ids[this.ids.length-1];
        this.$refs.bindCard.open();
      },


    }
  };
</script>



<style type="text/css">
  .el-form-item .el-select {
    width: 100%;
  }
</style>
